package com.lyj.thread.reentrantlock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock：
 *
 */
public class ReentrantLockDemo {
    // 票
    public static   int tickets = 10;

    public static ReentrantLock lock = new ReentrantLock();

    public static Condition condition1 = lock.newCondition();
    public static Condition condition2 = lock.newCondition();
    public static Condition condition3 = lock.newCondition();

//    public static void main(String[] args) {
//        ReentrantLockDemo demo = new ReentrantLockDemo();
//        Thread t1 = new MyReentrantLockTask(ReentrantLockDemo.lock , demo);
//        Thread t2 = new MyReentrantLockTask(ReentrantLockDemo.lock , demo);
//        Thread t3 = new MyReentrantLockTask(ReentrantLockDemo.lock , demo);
//        t1.setName("张三");
//        t2.setName("李四");
//        t3.setName("王五");
//        t1.start();
//        t2.start();
//        t3.start();
//    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockDemo demo = new ReentrantLockDemo();

        new Thread(demo::method1).start();

        new Thread(demo::method2).start();

        new Thread(demo::method3).start();

    }
    public void method1() {
        lock.lock();
        try{
            System.out.println("method1 准备处理");
            condition1.await();
            System.out.println("method1 ");
            condition2.signal();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void method2() {
        lock.lock();
        try{
            System.out.println("method2 准备处理");
            condition2.await();
            System.out.println("method2 ");
            condition3.signal();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void method3() {
        lock.lock();
        try{
            System.out.println("method3 准备处理");
            condition1.signal();
            condition3.await();
            System.out.println("method3 ");
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
